﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using log4net.Core;

namespace CYTS.Aoyou.Framework.Log
{
    internal class MessageLogImpl : LogImpl, IMessageLog
    {
        public MessageLogImpl(ILogger logger)
            : base(logger)
        {
        }

        public void Record(object message)
        {
            var context = CYTS.Aoyou.Framework.Context.ContextFactory.GetContext();
            if (context != null)
            {
                this.Record(message, context.UniqueID.ToString(), context.ClientAddress, context.ServerAddress, context.ServerName, context.ServiceName);
            }
            else
            {
                this.Record(message, string.Empty, string.Empty, string.Empty, Dns.GetHostName(), string.Empty);
            }
        }

        public void Record(object message, string uniqueID, string clientIP, string serverIP, string serverName, string serviceName)
        {
            if (this.IsInfoEnabled)
            {
                LoggingEvent loggingEvent = new LoggingEvent(typeof(MessageLogImpl), Logger.Repository, Logger.Name, Level.Info, message, null);
                loggingEvent.Properties["ClientIP"] = clientIP;
                loggingEvent.Properties["ServerIP"] = serverIP;
                loggingEvent.Properties["UniqueID"] = uniqueID;
                loggingEvent.Properties["ServerName"] = serverName;
                loggingEvent.Properties["ServiceName"] = serviceName;
                Logger.Log(loggingEvent);
            }
        }
    }
}
